package com.linkvin.ec.hcs.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.linkvin.ec.hcs.dao.model.mdm.Department;
import com.linkvin.ec.hcs.dao.model.mdm.bo.DepartmentBo;
import com.linkvin.ec.hcs.service.mdm.DepartmentService;
import com.linkvin.ec.hcs.vo.RestVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/department", produces = MediaType.APPLICATION_JSON_VALUE)
public class DepartmentController {

    private static final Logger LOGGER = LoggerFactory.getLogger(DepartmentController.class);

    @Autowired
    private DepartmentService departmentService;

    @PostMapping(value = "/queryPage")
    public RestVO<Page<DepartmentBo>> queryDepartmentList(@RequestBody Page<DepartmentBo> page){
        RestVO<Page<DepartmentBo>> result = new RestVO<>(0,"success");

        LOGGER.info("DepartmentController.queryDepartmentList params : {}", page);

        try{
            result.setData(departmentService.queryPageDepartment(page));
        }catch(Exception ex)
        {
            LOGGER.error("DepartmentController.queryRoleList ERROR Message : {} ", ex.getMessage());
            result.setCode(30003);
            result.setMessage("部门查询错误");
        }
        return result;
    }

    @PostMapping(value = "/create")
    public RestVO<DepartmentBo> create(@RequestBody Department department){
        RestVO<DepartmentBo> result = new RestVO<>(0,"success");

        LOGGER.info("DepartmentController.create params : {}", department);

        try {
            DepartmentBo departmentBO = departmentService.addDepartment(department);
            result.setData(departmentBO);
        }catch (Exception e){
            LOGGER.error("DepartmentController.create ERROR Message : {} ", e.getMessage());
            result.setCode(30004);
            result.setMessage(e.getMessage());
        }

        return result;
    }

    @PostMapping(value = "/update")
    public RestVO<DepartmentBo> update(@RequestBody Department department){
        RestVO<DepartmentBo> result = new RestVO<>(0,"success");
        LOGGER.info("DepartmentController.update params : {}", department);

        try {
            DepartmentBo departmentBO  = departmentService.updateDepartment(department);
            result.setData(departmentBO);
        }catch (Exception e){
            LOGGER.error("DepartmentController.update ERROR Message : {} ", e.getMessage());
            result.setCode(30004);
            result.setMessage(e.getMessage());
        }

        return result;
    }

    @PostMapping(value = "/delete")
    public RestVO<Boolean> deleteDepts(@RequestBody List<Integer> ids){

        RestVO<Boolean> result = new RestVO<>(0,"success");
        LOGGER.info("DepartmentController.update params : {}", ids);

        try {
            int i =  departmentService.deleteDepartments(ids);
            Boolean blnrtn = i > 0;
            result.setData(blnrtn);
        }catch (Exception e){
            LOGGER.error("DepartmentController.deleteDepts ERROR Message : {} ", e.getMessage());
            result.setData(false);
            result.setCode(30004);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

}
